const app = getApp()
const http = require('../../utils/http.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        showNav: false
    },

    /**
     * 到图说列表
     */
    goPicDesc() {
        let picDesc = this.data.picDesc
      console.log(picDesc)
        wx.navigateTo({
            url: '/pages/index/picDesc/picDesc?fid=' + picDesc.fid
        })
    },
    // 获取轮播文章详情
      article(index){
      console.log(index)
      var  url = 'Article_articleInfo';
      var token = wx.getStorageSync('token')
      var ids = index.currentTarget.dataset.id
      console.log(ids)
      var slideImg = this.data.slideImg
      console.log(slideImg)
      var id = slideImg[ids].url
      console.log(id)
      http._post(
        url,
        {token:token,tid:id},
        res=>{
          console.log(id)
          var data =res.data
          console.log(data)
          // if(data.re==='1'){
            wx.navigateTo({
              url: '/pages/index/picDesc/picDdetail/picDdetail?id=' + id
            })
          // }
        }
      )
    },
    // 获取轮播视频详情
    lesson(index){
        console.log(index)
        var url = 'Lesson_getVideourl'
        var token = wx.getStorageSync('token')
        var ids = index.currentTarget.dataset.id
        console.log(ids)
        var slideImg = this.data.slideImg
        console.log(slideImg)
        var id = slideImg[ids].url
        console.log(id)
        http._post(
          url,
          { token: token, lid: id},
          res=>{
            var data= res.data
            console.log(data)
            wx.navigateTo({
              url: '/pages/course/course?id=' + id,
            })
          }
        )

    },

    //获取AR功能
    lc:function(e){
        console.log(e)
        wx.showToast({
          title: '暂时没有开通AR',
          icon: 'none',
          duration: 2000
        })
    },

    /**
     * 到搜索页
     */
    goSearch() {
        wx.navigateTo({
            url: '/pages/index/search/search?type=2'
        })
    },

    /**
     * 大讲堂查看全部课程
     */
    goForumAll() {
        wx.navigateTo({
            url: '/pages/forum/forumAll/forumAll?type=' + 1
        })
    },

    /**
     * 资料精选查看全部课程
     */
    goMetAll() {
        wx.navigateTo({
            url: '/pages/forum/forumAll/forumAll?type=' + 3
        })
    },

    /**
     * 到课程详情
     */
    goCourseDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/course/course?id=' + id,
        })
    },

    /**
     * 到资料精选详情
     */
    goSoiDetail(e) {
        let id = http.dataIndex(e)[1]
        wx.navigateTo({
            url: '/pages/course/soiDetail/soiDetail?id=' + id,
        })
    },

    /**
     * 到数据服务列表
     */
    goDataList(e) {
      console.log(e)
        let id = http.dataIndex(e)[1]
      console.log(http.dataIndex(e))
        app.globalData.id = id
        wx.switchTab({
            url: '/pages/data/data'
        })
    },

    /**
     * 获取banner
     */
    // 首页的列表
    indexFn() {
        let url = 'Index_index';
        http._post(url, {}, res => {
            let data = res.data;
            if (data.re == 1) {
                let temp_star = []
                for (let item of data.data.lessonList) {
                    item.star = Number(item.star)
                    temp_star.push(item)
                }
                this.setData({
                    slideImg: data.data.bannerList,
                    picDesc: data.data.formData,
                    pubCList: temp_star,
                    dataServiceNav: data.data.shopTypeList,  
                    dataList:data.data.datumList,
                })
            } else {
                wx.showToast({
                    title: '2222222',
                    icon: 'none',
                    duration: 2000
                })
            }
        }, res => {

        })
    },


    /**
     * 扫描二维码
     */
    scanQrcode() {
        wx.scanCode({
          onlyFromCamera:true,
          scanType: ['barCode', 'qrCode'],
            success: function(res) {
                console.log(res)
            },
            fail: function(res) {
                console.log(res)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.getSystemInfo({
            success: function(res) {
                let version = res.SDKVersion
                version = version.replace(/\./g, "")
                if (parseInt(version) < 190) {
                    wx.showToast({
                        title: '为了更好的体验，请升级您的微信',
                        icon: 'none',
                        duration: 1000
                    })
                }
                return
            }
        })
      wx.showShareMenu({
        withShareTicket: true
      })
        const token = wx.getStorageSync('token')

        wx.hideTabBar({
            fail: function() {
                setTimeout(function() {
                    wx.hideTabBar()
                }, 500)
            }
        })
        app.editTabBar()
        this.indexFn();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 页面滚动事件
     */
    onPageScroll: function(Object) {
        // console.log(Object)
        let that = this
        if (Object.scrollTop > 100) {
            that.setData({
                showNav: true
            })
        } else {
            that.setData({
                showNav: false
            })
        }
    }
})